package com.bytech.admin.biz.controller.tag;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytech.admin.biz.exception.BlogAdminRuntimeException;
import com.bytech.common.biz.entity.article.ArticleDto;
import com.bytech.common.biz.entity.article.ArticleQuery;
import com.bytech.common.biz.entity.articleTag.ArticleTag;
import com.bytech.common.biz.entity.articleTag.ArticleTagDto;
import com.bytech.common.biz.entity.articleTag.ArticleTagQuery;
import com.bytech.common.biz.entity.base.BaseResult;
import com.bytech.common.biz.entity.base.BlogResultStatus;
import com.bytech.common.biz.enums.CommonStatusEnum;
import com.bytech.common.biz.service.remote.article.RemoteArticleService;
import com.bytech.common.utils.StringUtils;

/**
 * @Author Zhao
 * @Create 2021/8/10 18:33
 */
@Controller
@RequestMapping("/tag")
public class ArticleTagController {

    @Resource
    private RemoteArticleService remoteArticleService;

    @GetMapping("/list")
    public String toTagList(Model model) {
        model.addAttribute("states", CommonStatusEnum.validValues());
        return "/tag/tags";
    }

    @GetMapping("/save")
    public String toSaveTagPage(String tagId, Model model) {
        ArticleTagDto articleTag = new ArticleTagDto();
        if (!StringUtils.isBlank(tagId)) {
            BaseResult<ArticleTagDto> queryResult = remoteArticleService.getTagById(tagId);
            if (queryResult.isFailure()) {
                throw new BlogAdminRuntimeException(BlogResultStatus.ofCode(queryResult.getCode()));
            }
            articleTag = queryResult.getData();
        }
        model.addAttribute("articleTag", articleTag);
        model.addAttribute("tagStates", CommonStatusEnum.validValues());
        return "/tag/save";
    }

    @PostMapping("/save")
    @ResponseBody
    public BaseResult<ArticleTag> doSaveTag(ArticleTagDto tagDto) {
        return remoteArticleService.saveTag(tagDto);
    }

    @PostMapping("/delete")
    @ResponseBody
    public BaseResult<String> doDeleteTag(String id) {
        return remoteArticleService.deleteTagById(id);
    }

    @PostMapping("/toggleActive")
    @ResponseBody
    public BaseResult<String> doToggleTagActive(String id) {
        ArticleTagDto tag = remoteArticleService.getTagById(id).getData();
        if (tag == null) {
            return BaseResult.<String>error("标签不存在！").build();
        }
        CommonStatusEnum newStatus = tag.isNormal() ? CommonStatusEnum.FORBID : CommonStatusEnum.NORMAL;
        tag.setTagStatus(newStatus.getValue());
        remoteArticleService.saveTag(tag);
        return BaseResult.<String>success("保存成功！").build();
    }

    @GetMapping("/pageByQuery")
    @ResponseBody
    public BaseResult<Page<ArticleTagDto>> pageTagByQuery(ArticleTagQuery<ArticleTag> tagQuery) {
        return remoteArticleService.pageTagByQuery(tagQuery);
    }
}
